package com.mjfeed.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * @description 论坛话题
 * @author zhaominglei
 * @date 2015年1月9日
 */
@Entity
@Table(name="feed_forum_topic")
public class ForumTopic extends BaseEntity implements Serializable {
	private static final long serialVersionUID = 7592639158541706927L;
	public static final int FORUM_TOPIC_TYPE = 0;
	public static final int COLLEGE_TOPIC_TYPE = 1;
	
	/** 类型 0论坛 1学院 **/
	private Integer type;
	/** 标题 **/
	private String title;
	/** 原图片 **/
	private String source;
	/** 介绍 **/
	private String introduction;
	/** 内容 **/
	private String content;
	/** 浏览次数 **/
	private Integer viewCount;
	/** 回复次数 **/
	private Integer replyCount;
	/** ip **/
	private String ip;
	/** 状态 **/
	private Integer status;
//	/** 第一个回复 **/
//	private Long firstReplyId;
//	/** 最后一个回复 **/
//	private Long lastReplyId;
	/** 商品分类 **/
	private ProductCategory productCategory;
	/** 会员 **/
	private Member member;
	/** 文件 */
	private MultipartFile file;
	
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getSource() {
		return source;
	}
	public void setSource(String source) {
		this.source = source;
	}
	public String getIntroduction() {
		return introduction;
	}
	public void setIntroduction(String introduction) {
		this.introduction = introduction;
	}
	@Column(name="content", columnDefinition="LONGTEXT")
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	@Column(name="view_count")
	public Integer getViewCount() {
		return viewCount;
	}
	public void setViewCount(Integer viewCount) {
		this.viewCount = viewCount;
	}
	@Column(name="reply_count")
	public Integer getReplyCount() {
		return replyCount;
	}
	public void setReplyCount(Integer replyCount) {
		this.replyCount = replyCount;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="product_category_id")
	public ProductCategory getProductCategory() {
		return productCategory;
	}
	public void setProductCategory(ProductCategory productCategory) {
		this.productCategory = productCategory;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="member_id", nullable = false, updatable = false)
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	@Transient
	public MultipartFile getFile() {
		return file;
	}
	public void setFile(MultipartFile file) {
		this.file = file;
	}
	/**
	 * 判断是否为空
	 * 
	 * @return 是否为空
	 */
	@Transient
	public boolean isEmpty() {
		return (getFile() == null || getFile().isEmpty()) && StringUtils.isEmpty(getSource());
	}
	
	/**
	 * 持久化前处理
	 */
	@PrePersist
	public void prePersist() {
		if (getViewCount() == null) {
			setViewCount(1);
		}
		if (getReplyCount() == null) {
			setReplyCount(0);
		}
		if (getStatus() == null) {
			setStatus(0);
		}
	}
	
	/**
	 * 更新前处理
	 */
	@PreUpdate
	public void preUpdate() {
		if (getViewCount() == null) {
			setViewCount(1);
		}
		if (getReplyCount() == null) {
			setReplyCount(0);
		}
		if (getStatus() == null) {
			setStatus(0);
		}
	}
}
